package cn.edu.hznu.mybatis.proxy;

import cn.edu.hznu.mybatis.config.Configuration;
import cn.edu.hznu.mybatis.config.Mapper;
import cn.edu.hznu.mybatis.utils.Executor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;

/**
 * Created by wjj on 2020/5/25
 */
public class MapperProxy implements InvocationHandler {
    private Configuration cfg;
    private Connection conn;

    public MapperProxy(Configuration cfg, Connection conn) {
        this.cfg = cfg;
        this.conn = conn;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开始查询");
        //获取方法名
        String methodName=method.getName();
        //获取方法的类
        String classname=method.getDeclaringClass().getName();
        //组合key
        Mapper mapper = cfg.getMappers().get(classname + "." + methodName);
        if (mapper==null)
            throw  new IllegalArgumentException("掺入参数有误");
        //调用工具类，查询所有
        return new Executor().selectList(mapper,conn);
    }
}
